package com.gzx.file.service;

import com.gzx.file.constant.FileUploadUrl;
import com.gzx.file.utils.FileKit;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service("fileService")
public class FileServiceImpl implements FileService {
    /**
     * 根域名
     */
    @Value("${param.domain.url}")
    private String domainUrl;

    /**
     * 实际保存地址
     */
    @Value("${param.file.save.url}")
    private String saveUrl;

    @Override
    public List<String> saveFile(int fileType, List<MultipartFile> multipartFiles) throws IOException {
        // 文件分类路径，例 /image
        String classifyUrl = _fileClassify(fileType);
        List<String> fileUrls = new ArrayList<>();
        for (MultipartFile multipartFile : multipartFiles) {
            String fileName = FileKit.saveToUuidFile(multipartFile, saveUrl + "/" + classifyUrl);
            // 真实路径是 saveUrl + classifyUrl + file
            // 返回地址是 domainUrl + classifyUrl + file
            fileUrls.add(getFileUrl(classifyUrl, fileName));
        }
        return fileUrls;
    }

    /**
     * 生成文件路径
     * @param classifyUrl
     * @param fileName
     * @return
     */
    private String getFileUrl(String classifyUrl, String fileName) {
        return domainUrl + "/" + classifyUrl + "/" + fileName;
    }

    /**
     * 生成真实文件路径
     * @param classifyUrl
     * @param fileName
     * @return
     */
    private String getRealityFileUrl(String classifyUrl, String fileName) {
        return saveUrl + "/" + classifyUrl + "/" + fileName;
    }

    @Override
    public File getFile(int fileType, String fileName){
        String fileRelativePath = this._fileClassify(fileType);
        String fileUrl = getRealityFileUrl(fileRelativePath, fileName);
        File file = new File(fileUrl);
        if (!file.exists()) {
            throw new RuntimeException("文件不存在");
        }
        return file;
    }

    @Override
    public boolean deleteFile(String fileUrl) {
        return false;
    }

    @Override
    public boolean updateFile(String fileUrl, MultipartFile multipartFiles) {
        return false;
    }

    /**
     * 区分不同文件路径
     *
     * @param fileType 文件类型
     * @return 文件的url
     */
    private String _fileClassify(int fileType) {
        String fileRelativePath = null;
        switch (fileType) {
            case 0:
                fileRelativePath = FileUploadUrl.IMAGE_FILE_PATH.getValue();
                break;
            case 1:
                fileRelativePath = FileUploadUrl.VIDEO_FILE_PATH.getValue();
                break;
            case 2:
                fileRelativePath = FileUploadUrl.DATA_FILE_PATH.getValue();
                break;
            case 3:
                fileRelativePath = FileUploadUrl.AUDIO_FILE_PATH.getValue();
                break;
            default:
                fileRelativePath = FileUploadUrl.COMMON_FILE_PATH.getValue();
        }
        return fileRelativePath;
    }
}
